﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AzureDiagnostics2Excel
{
    public partial class AccountSettingsWindow : Form
    {
        public AccountSettingsWindow()
        {
            InitializeComponent();
            UpdateAccounts();
        }

        private void listBoxAccounts_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (this.listBoxAccounts.SelectedIndex >= 0)
            {
                Properties.Settings.Default.Properties.Remove(this.listBoxAccounts.SelectedValue.ToString().Substring(43).Split(';').First());
                Properties.Settings.Default.Save();
                this.listBoxAccounts.DataSource = null;
                UpdateAccounts();
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            AddAccountWindow dialog = new AddAccountWindow();

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.listBoxAccounts.DataSource = null;
                UpdateAccounts();
            }

            dialog.Close();
            dialog.Dispose();
        }

        private void UpdateAccounts()
        {
            List<AccountSetting> accountSettings = new List<AccountSetting>();
            foreach (System.Configuration.SettingsProperty property in Properties.Settings.Default.Properties)
            {
                accountSettings.Add(new AccountSetting { AccountName = property.Name, ConnectionString = property.DefaultValue.ToString() });
            }
            this.listBoxAccounts.DataSource = accountSettings;
            this.listBoxAccounts.DisplayMember = "AccountName";
            this.listBoxAccounts.ValueMember = "ConnectionString";
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
